home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8352 / 8352.xpi / chrome / greasefire.jar / content / browser.js < prev    next >
Text File  |  2009-05-24  |  4KB  |  132 lines

  1. /*
  2.  * Copyright (C) 2008 by Steve Krulewitz <skrulx@gmail.com>
  3.  * Licensed under GPLv2 or later, see file LICENSE in the xpi for details.
  4.  */
  5. if(!("Cc" in window))
  6.   window.Cc = Components.classes;
  7. if(!("Ci" in window))
  8.   window.Ci = Components.interfaces;
  9.  
  10. var GreasefireController = {
  11.  
  12.   _XUL_NS: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
  13.   _ios: null,
  14.   _gfs: null,
  15.   _appContext: null,
  16.   _menuItem: null,
  17.   _currentResults: null,
  18.   _currentURI: null,
  19.   _monkey: null,
  20.  
  21.   init: function() {
  22.     this._ios = Cc["@mozilla.org/network/io-service;1"]
  23.                   .getService(Ci.nsIIOService);
  24.     this._gfs = Cc["@skrul.com/greasefire/service;1"]
  25.                   .getService(Ci.gfIGreasefireService);
  26.  
  27.     window.addEventListener("load", this, false);
  28.     window.addEventListener("unload", this, false);
  29.   },
  30.  
  31.   _newLocation: function(aURI) {
  32.     this._currentURI = aURI;
  33.     if (aURI) {
  34.       this._currentResults = this._gfs.search(aURI);
  35.     }
  36.     else {
  37.       this._currentResults = null;
  38.     }
  39.  
  40.     this._updateMenu();
  41.   },
  42.  
  43.   _setupMenu: function() {
  44.     var popup = document.getElementById("gm-status-popup");
  45.     popup.insertBefore(
  46.       document.createElementNS(this._XUL_NS, "menuseparator"),
  47.       popup.firstChild);
  48.     this._menuItem = document.createElementNS(this._XUL_NS, "menuitem");
  49.     // Use "onmouseup" here to trigger the picker window.  This was previously
  50.     // "oncommand" but this caused a strange bug (only on windows) where right
  51.     // clicking on an installed script in the same popup menu would cause the
  52.     // oncommand event to be triggered.
  53.     this._menuItem.setAttribute("onmouseup",
  54.                                 "GreasefireController.openResults()");
  55.     popup.insertBefore(this._menuItem, popup.firstChild);
  56.  
  57.     this._monkey = document.getElementById("gm-status-image");
  58.   },
  59.  
  60.   _updateMenu: function() {
  61.  
  62.     var count = this._currentResults ? this._currentResults.length : 0;
  63.     var label;
  64.     switch(count) {
  65.     case 0:
  66.       label = "No scripts available";
  67.       break;
  68.     case 1:
  69.       label = "1 script available";
  70.       break;
  71.     default:
  72.       label = count + " scripts available";
  73.     }
  74.  
  75.     this._menuItem.setAttribute("label", label);
  76.     this._menuItem.setAttribute("disabled", count == 0 ? "true" : "false");
  77.  
  78.     var classList = this._monkey.className.split(" ");
  79.     classList = classList.filter(function(e) {
  80.       return e != "scripts-available";
  81.     });
  82.  
  83.     if (count > 0) {
  84.       classList.push("scripts-available");
  85.     }
  86.     this._monkey.className = classList.join(" ");
  87.   },
  88.  
  89.   openResults: function() {
  90.  
  91.     if (!this._currentResults) {
  92.       return;
  93.     }
  94.  
  95.     var params = Cc["@mozilla.org/embedcomp/dialogparam;1"]
  96.                    .createInstance(Ci.nsIDialogParamBlock);
  97.     var array = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
  98.     array.appendElement(this._currentResults, false);
  99.     array.appendElement(this._currentURI, false);
  100.     params.objects = array;
  101.  
  102.     openDialog("chrome://greasefire/content/picker.xul",
  103.                "",
  104.                "chrome,dialog=no,resizable",
  105.                params);
  106.   },
  107.  
  108.   handleEvent: function(aEvent) {
  109.  
  110.     if (aEvent.type == "load") {
  111.       this._setupMenu();
  112.  
  113.       gBrowser.addProgressListener(this,
  114.                                    Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
  115.     }
  116.   },
  117.  
  118.   // nsIWebProgress
  119.   onLocationChange: function(aProgress, aRequest, aURI)
  120.   {
  121.     this._newLocation(aURI);
  122.   },
  123.  
  124.   onStateChange: function() {},
  125.   onProgressChange: function() {},
  126.   onStatusChange: function() {},
  127.   onSecurityChange: function() {},
  128.   onLinkIconAvailable: function() {}
  129. }
  130.  
  131. GreasefireController.init();
  132.